﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dotnetCHARTING.WinForms;
using ViCode_LeVi.Data;
using System.Runtime.InteropServices;
using ViCode_LeVi.Controls.Logs;
using ViCode_LeVi.Configs;

namespace ViCode_LeVi.Controls.Charts
{
    public enum eChartLabelMode
    {
        Full,
        FullWithValue,
        Short,
        ShortWithValue
    }
    public enum AmpelType
    {
        UNKNOWN = 1,
        RED = 0,
        YELLOW = 2,
        GREEN = 3,
    }
    public enum eChartLegendMode
    {
        None,
        Full,
        Short
    }
    public enum eChartToolTipMode
    {
        None,
        Full,
        Percent,
        FullPercent
    }
    public enum eChartType
    {
        BarChartImportance,
        BarChartQQS,
        PotentialPortfolioChart
    }
    public enum eImportanceMode
    {
        Absolute,
        Relativ
    }
    public enum FactorType
    {
        UNKNOWN = 0,
        GP = 2,
        GE = 4,
        HK = 8,
        SK = 16,
        BK = 32,
    }
    [Serializable]
    public enum ActionState
    {
        PLANNED = 0,
        INPROGRESS = 1,
        FINISHED = 2,
    }
    public class FactorType_Helper
    {
        public static string GetFactorTypeString(FactorType eFactorType)
        {
            FactorType type = eFactorType;
            if (type != FactorType.HK)
            {
                if (type == FactorType.SK)
                {
                    return RootData.Intance.Report_SucessFactor_Structural;
                }
                if (type == FactorType.BK)
                {
                    return RootData.Intance.Report_SucessFactor_Relational;
                }
            }
            else
            {
                return RootData.Intance.Report_SucessFactor_Human;
            }
            return string.Empty;

            //string str = string.Empty;
            //try
            //{
            //    FactorType type = eFactorType;
            //    if (type != FactorType.HK)
            //    {
            //        if (type != FactorType.SK)
            //        {
            //            if (type == FactorType.BK)
            //            {
            //                return "Relational Capital";//"Relational Capital"
            //            }
            //            goto Label_002E;
            //        }
            //    }
            //    else
            //    {
            //        return "Human Capital";//"Human Capital"
            //    }
            //    return "Structural Capital";//"Structural Capital"
            //Label_002E:
            //    str = string.Empty;
            //}
            //catch (Exception exception)
            //{
            //    Log.LogException(exception);
            //}
            //return str;
        }
    }
    public enum eWBDimension
    {
        Quantity,
        Quality,
        Systematic
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct FactorProperty
    {
        public string Id;
        public string Name;
        public FactorType Type;
        public int XPercent;
        public int YPercent;
        public int BubblePercent;
        public string Marker;
        public double XPotential;
        public double YPotential;
        public int EvaluatedDimensions;
        public int Importance;
        public double ImportancePercent;
        public double Quantity;
        public double Quality;
        public double Systematic;
        public double BubbleSize;
    }
    
    public partial class ChartCtrl : UserControl
    {
        public ChartCtrl()
        {
            InitializeComponent();
            this.Load += new EventHandler(User_VMTabAnalysis_Tab3_Load);
        }

        void User_VMTabAnalysis_Tab3_Load(object sender, EventArgs e)
        {
            CreateChart();   
        }
        private bool m_bFilterFactorTypeBK = true;
        private bool m_bFilterFactorTypeGE = true;
        private bool m_bFilterFactorTypeGP = true;
        private bool m_bFilterFactorTypeHK = true;
        private bool m_bFilterFactorTypeSK = true;
        private bool m_bLabelWithPercent;
        private bool m_bLegendWithPercent;
        private bool m_bLimitLegendTextLength;
        private bool m_bUpdateLimitLegendTextLength = true;
        private bool m_bUpdateMaxLegendTextLength = true;
        private Color m_colGradFrom = Color.FromArgb(0xf8, 0xd9, 0xc1);
        private Color m_colGradTo = Color.FromArgb(0xd6, 0xfd, 0xd5);
        private eChartLabelMode m_eChartLabelMode = eChartLabelMode.Short;
        private eChartLegendMode m_eChartLegendMode = eChartLegendMode.Full;
        private eChartToolTipMode m_eChartToolTipMode;
        private dotnetCHARTING.WinForms.Orientation m_eExtraLegendBoxOrientation = dotnetCHARTING.WinForms.Orientation.Bottom;
        private int m_iMaxLegendTextLength = 50;
        private int m_iNumberOfElements;
        private ContextMenuStrip m_oContextMenu;
        private RootData m_oWorkingProject;
        protected string m_sXAxisLabel = "X-Achse";
        protected string m_sYAxisLabel = "Y-Achse";

        public Color ChartBackColor
        {
            get
            {
                return this.chart1.Background.Color;
            }
            set
            {
                this.chart1.Background.Color = value;
            }
        }

        public int ChartDPI
        {
            get
            {
                return this.chart1.Dpi;
            }
            set
            {
                this.chart1.Dpi = value;
            }
        }

        public eChartLabelMode ChartLabelMode
        {
            get
            {
                return this.m_eChartLabelMode;
            }
            set
            {
                this.m_eChartLabelMode = value;
            }
        }

        public eChartLegendMode ChartLegendMode
        {
            get
            {
                return this.m_eChartLegendMode;
            }
            set
            {
                this.m_eChartLegendMode = value;
            }
        }

        public eChartToolTipMode ChartToolTipMode
        {
            get
            {
                return this.m_eChartToolTipMode;
            }
            set
            {
                this.m_eChartToolTipMode = value;
            }
        }

        public dotnetCHARTING.WinForms.Orientation ExtraLegendBoxOrientation
        {
            get
            {
                return this.m_eExtraLegendBoxOrientation;
            }
            set
            {
                this.m_eExtraLegendBoxOrientation = value;
            }
        }

        public bool FilterFactorTypeBK
        {
            get
            {
                return this.m_bFilterFactorTypeBK;
            }
            set
            {
                this.m_bFilterFactorTypeBK = value;
            }
        }

        public bool FilterFactorTypeGE
        {
            get
            {
                return this.m_bFilterFactorTypeGE;
            }
            set
            {
                this.m_bFilterFactorTypeGE = value;
            }
        }

        public bool FilterFactorTypeGP
        {
            get
            {
                return this.m_bFilterFactorTypeGP;
            }
            set
            {
                this.m_bFilterFactorTypeGP = value;
            }
        }

        public bool FilterFactorTypeHK
        {
            get
            {
                return this.m_bFilterFactorTypeHK;
            }
            set
            {
                this.m_bFilterFactorTypeHK = value;
            }
        }

        public bool FilterFactorTypeSK
        {
            get
            {
                return this.m_bFilterFactorTypeSK;
            }
            set
            {
                this.m_bFilterFactorTypeSK = value;
            }
        }

        public bool GetLimitLegendTextLength
        {
            get
            {
                if (this.m_bUpdateLimitLegendTextLength)
                {
                    this.m_bLimitLegendTextLength = false;// Config.Charts.LimitLegendTextLength; toto
                    this.m_bUpdateLimitLegendTextLength = false;
                }
                return this.m_bLimitLegendTextLength;
            }
        }

        public int GetMaxLegendTextLength
        {
            get
            {
                if (this.m_bUpdateMaxLegendTextLength)
                {
                    this.m_iMaxLegendTextLength = 100;// Config.Charts.MaxLegendTextLength; todo
                    this.m_bUpdateMaxLegendTextLength = false;
                }
                return this.m_iMaxLegendTextLength;
            }
        }

        public bool LabelWithPercent
        {
            get
            {
                return this.m_bLabelWithPercent;
            }
            set
            {
                this.m_bLabelWithPercent = value;
            }
        }

        private int LegendBoxHeight
        {
            get
            {
                int height = 0;
                try
                {
                    if (this.chart1.ExtraLegendBoxes.Count > 0)
                    {
                        height = this.chart1.ExtraLegendBoxes[0].GetRectangle().Height;
                    }
                }
                catch (Exception exception)
                {
                    Log.LogException(exception);
                }
                return height;
            }
        }

        public bool LegendWithPercent
        {
            get
            {
                return this.m_bLegendWithPercent;
            }
            set
            {
                this.m_bLegendWithPercent = value;
            }
        }

        public int NumberOfElements
        {
            get
            {
                return this.m_iNumberOfElements;
            }
            set
            {
                this.m_iNumberOfElements = value;
            }
        }

        public bool UpdateConfig
        {
            set
            {
                this.m_bUpdateLimitLegendTextLength = value;
                this.m_bUpdateMaxLegendTextLength = value;
            }
        }

        public virtual bool CreateChart()
        {
            return true;
        }
        protected void SetChartBasis(eChartType peChartType, string psXAxisLabel, string psYAxisLabel)
        {
            try
            {
                this.chart1.ToolTip.AutoPopDelay = 100;// Config.UI.ToolTipDuration; todo
                this.chart1.ToolTip.InitialDelay = 100;// Config.UI.ToolTipInitialDelay; todo
                this.chart1.ToolTip.UseAnimation = false;
                this.chart1.ToolTip.ShowAlways = true;
                switch (peChartType)
                {
                    case eChartType.BarChartImportance:
                    case eChartType.BarChartQQS:
                        this.chart1.Type = ChartType.ComboHorizontal;
                        this.chart1.DefaultElement.Transparency = VMConfig.Intance.Charts.BarChartTransparency; 
                        this.chart1.TitleBox.Position = TitleBoxPosition.FullWithLegend;
                        this.chart1.TitleBox.Label.Alignment = StringAlignment.Center;
                        this.chart1.TitleBox.CornerTopLeft = BoxCorner.Round;
                        this.chart1.TitleBox.Label.Font = new Font("Arial", 10f, FontStyle.Bold);
                        this.chart1.LegendBox.Template = "%icon %name";
                        this.chart1.LegendBox.LabelStyle.Font = new Font("Arial", 9f, FontStyle.Regular);
                        this.chart1.LegendBox.LabelStyle.Color = Color.Black;
                        this.chart1.ShadingEffect = true;
                        this.chart1.ShadingEffectMode = ShadingEffectMode.Two;
                        this.chart1.YAxis.TickLabelMode = TickLabelMode.Wrapped;
                        this.chart1.YAxis.TickLabelSeparatorLine.Color = Color.Black;
                        this.chart1.DefaultSeries.DefaultElement.HatchColor = Color.FromArgb(100, Color.Gray);
                        break;

                    case eChartType.PotentialPortfolioChart:
                        this.chart1.Title = ProjectData.Intance.Period.Tab6_Evaluation_Analysis_3;
                        this.chart1.XAxis.Label.Alignment = StringAlignment.Center;
                        this.chart1.YAxis.Label.Alignment = StringAlignment.Center;
                        this.chart1.XAxis.Label.Text = psXAxisLabel;
                        this.chart1.XAxis.DefaultTick.Label.Text = "%Value%";
                        this.chart1.XAxis.Label.Font = new Font("Arial", 10f, FontStyle.Bold);
                        this.chart1.XAxis.Label.Color = Color.Black;
                        this.chart1.YAxis.Label.Text = psYAxisLabel;
                        this.chart1.YAxis.DefaultTick.Label.Text = "%Value%";
                        this.chart1.YAxis.Label.Font = new Font("Arial", 10f, FontStyle.Bold);
                        this.chart1.YAxis.Label.Color = Color.Black;
                        this.chart1.DefaultElement.Transparency = 50;// Config.Charts.BubbleChartTransparency; todo
                        this.chart1.TitleBox.Position = TitleBoxPosition.FullWithLegend;
                        this.chart1.TitleBox.Label.Alignment = StringAlignment.Center;
                        this.chart1.TitleBox.CornerTopLeft = BoxCorner.Round;
                        this.chart1.TitleBox.Label.Font = new Font("Arial", 10f, FontStyle.Bold);
                        this.chart1.LegendBox.Template = "%Icon %Name";
                        this.chart1.LegendBox.LabelStyle.Font = new Font("Arial", 9f, FontStyle.Regular);
                        this.chart1.LegendBox.LabelStyle.Color = Color.Black;
                        this.chart1.ShadingEffectMode = ShadingEffectMode.One;
                        break;
                }
                this.UpdateConfig = true;
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
        }
        protected virtual SeriesCollection SetData(ref int riNumberOfElements)
        {
            return null;
        }
        protected eImportanceMode MapImportanceModeString(string sOption)
        {
            eImportanceMode Absolute = eImportanceMode.Absolute;
            try
            {
                switch (sOption)
                {
                    case "Absolute":
                        return eImportanceMode.Absolute;

                    case "relativ":
                        return eImportanceMode.Relativ;
                }
                Absolute = eImportanceMode.Absolute;
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
            return Absolute;
        }
        protected void WriteGeneralDisplayOptions(eChartType peChartType)
        {
            RootData poProject = RootData.Intance;
            try
            {
                //Todo
                //poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".ChartLabelMode"] = this.MapChartLabelMode(this.ChartLabelMode);
                //poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".LabelWithPercent"] = MapBoolOption(this.LabelWithPercent);
                //poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".ChartLegendMode"] = this.MapChartLegendMode(this.ChartLegendMode);
                //poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".LegendWithPercent"] = MapBoolOption(this.LegendWithPercent);
                //poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".ChartToolTipMode"] = this.MapChartToolTipMode(this.ChartToolTipMode);
                //poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".FilterFactorTypeHK"] = MapBoolOption(this.FilterFactorTypeHK);
                //poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".FilterFactorTypeBK"] = MapBoolOption(this.FilterFactorTypeBK);
                //poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".FilterFactorTypeSK"] = MapBoolOption(this.FilterFactorTypeSK);
                //poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".FilterFactorTypeGP"] = MapBoolOption(this.FilterFactorTypeGP);
                //poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".FilterFactorTypeGE"] = MapBoolOption(this.FilterFactorTypeGE);
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
        }
        protected void ReadGeneralDisplayOptions(eChartType peChartType)
        {
            RootData poProject = RootData.Intance;
            try
            {
                //Todo
                //this.ChartLabelMode = this.MapChartLabelModeString(poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".ChartLabelMode"], eChartLabelMode.Short);
                //this.LabelWithPercent = MapBoolOptionString(poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".LabelWithPercent"], false);
                //this.ChartLegendMode = this.MapChartLegendModeString(poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".ChartLegendMode"]);
                //this.LegendWithPercent = MapBoolOptionString(poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".LegendWithPercent"], false);
                //this.ChartToolTipMode = this.MapChartToolTipModeString(poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".ChartToolTipMode"]);
                //this.FilterFactorTypeHK = MapBoolOptionString(poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".FilterFactorTypeHK"], true);
                //this.FilterFactorTypeBK = MapBoolOptionString(poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".FilterFactorTypeBK"], true);
                //this.FilterFactorTypeSK = MapBoolOptionString(poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".FilterFactorTypeSK"], true);
                //this.FilterFactorTypeGP = MapBoolOptionString(poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".FilterFactorTypeGP"], false);
                //this.FilterFactorTypeGE = MapBoolOptionString(poProject.Params.ChartDisplayOptions[peChartType.ToString() + ".FilterFactorTypeGE"], false);
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
        }
        protected void GetAxisMarkers(Axis pAxis)
        {
            pAxis.ShowGrid = false;
            pAxis.AlternateGridBackground.Color = Color.Empty;
            AxisMarker marker = null;
            AxisMarker marker2 = null;
            AxisMarker marker3 = null;
            AxisMarker marker4 = null;
            AxisMarker marker5 = null;
            marker = new AxisMarker
            {
                LegendEntry = { Visible = false },
                ValueLow = 0,
                ValueHigh = 30,
                Background = new Background(VMConfig.Intance.Colors.ChartBad)
            };
            marker2 = new AxisMarker
            {
                LegendEntry = { Visible = false },
                ValueLow = 30,
                ValueHigh = 60,
                Background = new Background(VMConfig.Intance.Colors.ChartMedium)
            };
            marker3 = new AxisMarker
            {
                LegendEntry = { Visible = false },
                ValueLow = 60,
                ValueHigh = 0x66,
                Background = new Background(VMConfig.Intance.Colors.ChartGood)
            };
            marker4 = new AxisMarker
            {
                LegendEntry = { Visible = false },
                Value = 30,
                Line = new Line(Color.Black, 1, System.Drawing.Drawing2D.DashStyle.Dash)
            };
            marker5 = new AxisMarker
            {
                LegendEntry = { Visible = false },
                Value = 60,
                Line = new Line(Color.Black, 1, System.Drawing.Drawing2D.DashStyle.Dash)
            };
            pAxis.Markers.Add(new AxisMarker[] { marker, marker2, marker3, marker4, marker5 });
        }
#warning phia duoi thay toan True
        protected string GetFilterFactorTypeNames()
        {
            string str = "";
            try
            {
                //if (this.FilterFactorTypeGP)
                //{
                //    str = str + "Gesch\x00e4ftsprozesse,";
                //}
                //if (this.FilterFactorTypeGE)
                //{
                //    str = str + "Gesch\x00e4ftserfolge,";
                //}
                if (this.FilterFactorTypeHK)
                {
                    str = str + RootData.Intance.Report_SucessFactor_Human;// "Human Capital,";
                }
                if (this.FilterFactorTypeSK)
                {
                    str = str + RootData.Intance.Report_SucessFactor_Structural;//"Structural Capital,";
                }
                if (this.FilterFactorTypeBK)
                {
                    str = str + RootData.Intance.Report_SucessFactor_Relational;//"Relational Capital,";
                }
                if (str.EndsWith(","))
                {
                    str = str.Remove(str.Length - 1, 1);
                }
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
            return str;
        }
        protected static string GetDimensionDescription(eWBDimension peDimension)
        {
            string str = "";
            try
            {
                switch (peDimension)
                {
                    case eWBDimension.Quantity:
                        return "Quantity";

                    case eWBDimension.Quality:
                        return "Quality";

                    case eWBDimension.Systematic:
                        return "Systematic";
                }
                str = "Unbekannt";
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
            return str;
        }

        protected static string GetDimensionShortDescription(eWBDimension peDimension)
        {
            string str = "";
            try
            {
                switch (peDimension)
                {
                    case eWBDimension.Quantity:
                        return "Qn";

                    case eWBDimension.Quality:
                        return "Ql";

                    case eWBDimension.Systematic:
                        return "Sy";
                }
                str = "";
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
            return str;
        }

        public string FormatLegendEntryText(string psLegendEntryText)
        {
            string str = "";
            string str2 = "";
            int getMaxLegendTextLength = 0;
            int num2 = 50;
            try
            {
                str2 = psLegendEntryText;
                if (this.GetLimitLegendTextLength)
                {
                    getMaxLegendTextLength = this.GetMaxLegendTextLength;
                    if (str2.Length > getMaxLegendTextLength)
                    {
                        if (this.LegendWithPercent)
                        {
                            string[] strArray = str2.Split(new string[] { "(", ")" }, StringSplitOptions.RemoveEmptyEntries);
                            if (strArray[0].Length > getMaxLegendTextLength)
                            {
                                str2 = strArray[0].Substring(0, getMaxLegendTextLength - 3) + "... (" + strArray[1] + ")";
                            }
                        }
                        else
                        {
                            str2 = str2.Substring(0, getMaxLegendTextLength - 3) + "...";
                        }
                    }
                }
                while (str2.Length > 0)
                {
                    if (str2.Length > num2)
                    {
                        int index = str2.IndexOf(" ", (int)(num2 + 1));
                        if (index > 0)
                        {
                            if (str.Length > 0)
                            {
                                str = str + "\n" + str2.Substring(0, index);
                            }
                            else
                            {
                                str = str2.Substring(0, index);
                            }
                            str2 = str2.Substring(index + 1);
                        }
                        else
                        {
                            str = str + str2;
                            str2 = "";
                        }
                    }
                    else
                    {
                        if (str.Length > 0)
                        {
                            str = str + "\n" + str2;
                        }
                        else
                        {
                            str = str2;
                        }
                        str2 = "";
                    }
                }
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
            return str;
        }
    }
}
